package net.xdclass.exception;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.utils.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author ：zhouyang
 * @date ：Created in 2022/4/28 22:30
 * @modified By：
 * @version: $
 */
@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public JsonData handler(Exception e) {
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("业务异常");
            e.printStackTrace();
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMessage());
        } else {
            e.printStackTrace();
            log.error("系统异常");
            return JsonData.buildError("系统异常");
        }
    }
}
